package com.Servlet;

import com.Bean.Server;
import com.Util.ServerDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

@WebServlet(urlPatterns = "/addserver")
public class ParamServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String name = req.getParameter("name");
        String osname =req.getParameter("osname");
        String type = req.getParameter("kind");
String struct = new String();
String cpucore = new String();
String cpuhz = new String();
String memcap = new String();
String price = new String();

        switch(type){
            case "1":{
			 struct="lxc";
			 cpucore="1";
			 cpuhz="2.3Hz";
			 memcap="512M";
			 price="18";
			 break;
            }
	    case "2":{
			 struct="kvm";
			 cpucore="2";
			 cpuhz="2.3Hz";
			 memcap="1G";
			 price="43";
			 break;
	    }
	    case "3":{
			 struct="kvm";
			 cpucore="4";
			 cpuhz="3.3Hz";
			 memcap="2G";
			 price="120";
			 break;
	    }
	    case "4":{
			 struct="kvm";
			 cpucore="8";
			 cpuhz="3.3Hz";
			 memcap="4G";
			 price="240";
			 break;
	    }
        }

        Server server = new Server();
        server.setUsername(username);
        server.setName(name);
        server.setStruct(struct);
         server.setCpucore(cpucore);
        server.setCpuhz(cpuhz);
        server.setMemcap(memcap);
        server.setStatus("active");
        server.setOsname(osname);
        server.setPrice(price);


        Random r = new Random();
        String ip=r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256);
        server.setIpv4(ip);
        ServerDao.updateServer(server);
        resp.sendRedirect("./serve/myvps.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
